package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.FlowDataBean
import com.hjq.fcloud.bean.MettingData
import com.hjq.fcloud.event.EventBean
import com.hjq.fcloud.event.EventBusUtils
import com.hjq.fcloud.http.api.StoreSignDetailApi
import com.hjq.fcloud.http.api.StoreSignDetailSubmitApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.other.AppFilesHelper
import com.hjq.fcloud.other.SignRefershHelper
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback
import kotlinx.android.synthetic.main.activity_store_sign.batchSb
import kotlinx.android.synthetic.main.activity_store_sign.batchTimeSb
import kotlinx.android.synthetic.main.activity_store_sign.moneySb
import kotlinx.android.synthetic.main.activity_store_sign.numberSb
import kotlinx.android.synthetic.main.activity_store_sign.projectSb
import kotlinx.android.synthetic.main.activity_store_sign.sectionNameSb
import kotlinx.android.synthetic.main.activity_store_sign.supplierNameSb
import kotlinx.android.synthetic.main.activity_store_sign.totalSb
import kotlinx.android.synthetic.main.activity_store_sign.feedbackView
import kotlinx.android.synthetic.main.activity_store_sign.bottomView
import kotlinx.android.synthetic.main.activity_store_sign.feedbackRootView
import kotlinx.android.synthetic.main.activity_store_sign.flowView
import kotlinx.android.synthetic.main.activity_store_sign.fileView
import kotlinx.android.synthetic.main.activity_store_sign.statusLayout
import okhttp3.Call
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 入库凭证会签
 */
class StoreSignActivity : BaseDetailActivity() {

    companion object {
        //确认结果0未通过1通过
        const val STATUS_PASS = "1"
        const val STATUS_NOT_PASS = "0"

        fun start(ac: Context, jumpBean: CommonJumpBean) {
            var intent = Intent(ac, StoreSignActivity::class.java)
            intent.putExtra(INTENT_IN_ITEM, jumpBean)
            ac.startActivity(intent)
        }
    }

    private var status: String = STATUS_PASS

    private var mDetail:StoreSignDetailApi.Data? = null

    override fun userEventBus(): Boolean {
        return true
    }

    override fun getStateLayout(): View? {
        return statusLayout
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_store_sign
    }

    override fun initView() {

    }

    override fun initData() {
        getServerDetail()

        getServerFlow()
    }

    override fun handlerFlowRequestParms(parms:HashMap<String, Any?>){
        parms.put("biz_type","store")
    }

    fun handlerReject(view: View) {
        status = STATUS_NOT_PASS
        if (checkInput()) {
            FaceDetectRGBActivity.launcher(context, intent.extras)
        }
    }

    fun handlerConfirm(view: View) {
        status = STATUS_PASS
        if (checkInput()) {
            FaceDetectRGBActivity.launcher(context, intent.extras)
        }
    }

    private fun checkInput(): Boolean {
        var reason = feedbackView.getInPutText()
        if (TextUtils.isEmpty(reason)) {
            showToast("请输入意见反馈")
            return false
        }
        return true
    }

    override fun setFlowViewData(data: FlowDataBean?) {
//        if (canSignFromList) {
//            flowView.setVisibility(false)
//
//            //审核控件设置
//            feedbackRootView.setVisibility(true)
//            bottomView.setVisibility(true)
//        } else {
//            flowView.setVisibility(true)
//            flowView.setViewData(data?.setup_list)
//        }
    }


    private fun getServerDetail() {
        showAppLoading()
        var api = StoreSignDetailApi()
            .setId(commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(api)
            .request(object : HttpCallback<HttpData<StoreSignDetailApi.Data?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<StoreSignDetailApi.Data?>?) {
                    mDetail = data?.getData()
                    setViewData(mDetail)
                }

                override fun onEnd(call: Call?) {
                    super.onEnd(call)
                    showAppContent()
                }
            })
    }

    private fun setViewData(data: StoreSignDetailApi.Data?) {
        numberSb.setRightText(data?.fnc?.code)
        projectSb.setRightText(data?.fnc?.project?.project_name)
        sectionNameSb.setRightText(data?.fnc?.sect?.sect_name)
        moneySb.setRightText(data?.fnc?.money)
        supplierNameSb.setRightText(data?.fnc?.supply_corp?.corp_name)
        batchSb.setRightText(data?.batch_no)
        batchTimeSb.setRightText(data?.store_time)
        totalSb.setRightText(data?.money)
        
        fileView.setViewData(this, "材料入库凭证", AppFilesHelper.getAllFiles(data?.files))
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMainEventBus(bean: EventBean) {
        if (bean != null && bean.type == EventBusUtils.TYPE_FACE_AND_SIGN_FINISH) {
            var signParms = bean.parms as HashMap<String, String?>

            var parms = HashMap<String, Any?>()
            parms.putAll(signParms)
            requestServerSubmit(parms)
        }
    }


    private fun requestServerSubmit(parms: HashMap<String, Any?>) {
        parms.put("id", mDetail?.id)
        parms.put("reason", feedbackView.getInPutText())
        parms.put("result", status)
        EasyHttp.post(this)
            .api(StoreSignDetailSubmitApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<MettingData?>?>(this) {
                override fun onSucceed(data: HttpData<MettingData?>?) {
                    SignRefershHelper.signSuccessRefersh(context,commonJumpBean)
                    showToast(data?.getMessage())
                }
            })
    }

}